﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using LogWatcher.Common;

namespace LogWatcherX
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private String file;
        private FileWatcher fileWatcher;

        public MainWindow()
        {
            if (Application.Current.Properties["file"] == null)
            {
                file = LoadFileWindow.ShowLoadFileDialog();
                if (file == null)
                {
                    Application.Current.Shutdown(-1);
                }
            }
            else
            {
                file = (string) Application.Current.Properties["file"];
            }

            InitializeComponent();
            fileWatcher = new FileWatcher(file);
            fileWatcher.UpdateText +=new Action<string>(fileWatcher_UpdateText);

            HistoryFileManager.AddFileToHistoryFileList(file);

            Title += " - [" + file + "]";
        }

        private void fileWatcher_UpdateText(string obj)
        {
            Dispatcher.Invoke(new Action(() =>
                                             {
                                                 txtLog.SelectionChanged -= txtLog_SelectionChanged;
                                                 try
                                                 {
                                                     txtLog.Text = obj;
                                                     if (miFollowText.IsChecked)
                                                     {
                                                         txtLog.SelectionStart = txtLog.Text.Length;
                                                         txtLog.ScrollToEnd();
                                                     }
                                                 }
                                                 finally
                                                 {
                                                     txtLog.SelectionChanged += txtLog_SelectionChanged;
                                                 }
                                             }));
        }

        private void Clear_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            using (StreamWriter writer = new StreamWriter(new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.ReadWrite)))
            {
                writer.Write("");
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            fileWatcher.Start();
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            if (fileWatcher.IsStarted)
                fileWatcher.Stop();
        }

        private void miFollowText_Checked(object sender, RoutedEventArgs e)
        {
            if (!IsLoaded)
                return;

            txtLog.SelectionStart = txtLog.Text.Length;
            txtLog.ScrollToEnd();
        }

        private void txtLog_SelectionChanged(object sender, RoutedEventArgs e)
        {
            miFollowText.IsChecked = false;
        }
    }
}
